package com.graduate.game.user.controller;

import com.alibaba.fastjson.JSON;
import com.graduate.game.user.pojo.conversation;
import com.graduate.game.user.pojo.relation;
import com.graduate.game.user.service.relationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.management.relation.RelationService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("/relation")
@CrossOrigin(origins = "*", maxAge = 3600)
public class relationController {
    @Autowired
    public relationService relationService;

    //添加关注关系
    @RequestMapping("/add")
    public String add(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        Map maps =(Map) JSON.parse(s1);
        relation relation = new relation();
        relation.setMe((String) maps.get("me"));
        relation.setHe((String) maps.get("he"));
        //me关注了he，me可以产看我关注的所有，并且知道谁关注了我
        relationService.addrelation(relation);
        return "关注成功";
    }

    //查看我关注的人
    @RequestMapping("/check")
    public List<relation> check(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        return relationService.checkmyrelation(s1);
    }

    //信息发送
    @RequestMapping("/send")
    public String send(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        System.out.println("信息发送："+s1);
        Map maps =(Map) JSON.parse(s1);
        conversation conversation = new conversation();
        conversation.setSender((String) maps.get("sender"));
        conversation.setContent((String) maps.get("content"));
        conversation.setTaker((String) maps.get("taker"));
        relationService.addConversation(conversation);
        return "发送成功";
    }

    //查看聊天记录
    @RequestMapping("/looksend")
    public List<conversation> looksend(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        System.out.println(s1);
        Map maps =(Map) JSON.parse(s1);
        conversation conversation = new conversation();
        conversation.setSender((String) maps.get("sender"));
        conversation.setTaker((String) maps.get("taker"));
        System.out.println(relationService.checkcontConversations(conversation));
        return relationService.checkcontConversations(conversation);
    }
}
